<template>
    <!-- 可视化页面 -->
    <div class="user-content">
        <div id="chart"></div>
        <div class="content">
            <div id="left"></div>
            <div id="right"></div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { onMounted } from 'vue';
import { EChartsOption, init } from 'echarts';

interface DataType {
    id: string;
    //靶场名字
    name: string;
    //靶场封面
    cover: string;
    //靶场难度
    level: "简单" | "中等" | "高难";
    //靶场分类
    category: string;
    //靶场文件类型
    fileType: '镜像文件' | 'war' | 'zip' | '普通文件';
    //被谁创建的id(创建者可以删除自己创建的靶场)
    createBy: string;
}

//模拟的假数据
const data: DataType[] = [
    {
        id: "1",
        name: '靶场名称1',

        cover:
            'https://img1.baidu.com/it/u=2863213704,737400670&fm=253&fmt=auto&app=138 & f=JPEG?w=750 & h=500',
        level: "简单",
        category: 'SQL注入类',
        fileType: "镜像文件",
        createBy: "1"
    },
    {
        id: "2",
        name: '靶场名称2',
        cover:
            'https://img1.baidu.com/it/u=2863213704,737400670&fm=253&fmt=auto&app=138 & f=JPEG?w=750 & h=500',
        level: "中等",
        category: '扫描类',
        fileType: "war",
        createBy: "2"
    },
    {
        id: "3",
        name: '靶场名称3',
        cover:
            'https://img1.baidu.com/it/u=2863213704,737400670&fm=253&fmt=auto&app=138 & f=JPEG?w=750 & h=500',
        level: "高难",
        category: '文件类',
        fileType: "zip",
        createBy: "3"
    },
    {
        id: "4",
        name: '靶场名称4',
        cover:
            'https://img1.baidu.com/it/u=2863213704,737400670&fm=253&fmt=auto&app=138 & f=JPEG?w=750 & h=500',
        level: "高难",
        category: '文件类',
        fileType: "zip",
        createBy: "4"
    },
]



onMounted(() => {
    // 分类的类型
    let Arr: any = [];
    const arr: any = []
    const numarr:any =[]
    data.forEach((item) => {
        Arr.push(item.category)
    })
    console.log(Arr);
    
    Arr.forEach((ite: any) => {
        // 定义一个变量放索引
        let index = arr.findIndex((v: any) => {
            return ite == v.name
        })
        index === -1 ? arr.push({ value: 1, name: ite }) : arr[index].value++
    })
    console.log(arr);
    const arr1:any =[]
    const arr2:any =[]
    arr.forEach((item:any)=>{
        arr1.push(item.name)
    })
    console.log(arr1);
    arr.forEach((item:any)=>{
        arr2.push(item.value)
    })
    console.log(arr2);

    


    // 格式
    let categorylArr: any = [];
    const categoryarr: any = []
    data.forEach((item) => {
        categorylArr.push(item.fileType)
    })
    categorylArr.forEach((ite: any) => {
        // 定义一个变量放索引
        let index = categoryarr.findIndex((v: any) => {
            return ite == v.name
        })
        index === -1 ? categoryarr.push({ value: 1, name: ite }) : categoryarr[index].value++
    })


    // 难度
    let levelArr: any = [];
    const levelarr: any = []
    data.forEach((item) => {
        levelArr.push(item.level)
    })
    levelArr.forEach((ite: any) => {
        // 定义一个变量放索引
        let index = levelarr.findIndex((v: any) => {
            return ite == v.name
        })
        index === -1 ? levelarr.push({ value: 1, name: ite }) : levelarr[index].value++
    })


    // 获取dom，断言HTMLElement类型，否则会报错
    const chartEle: HTMLElement = document.getElementById('chart') as HTMLElement;
    const chartEle1: HTMLElement = document.getElementById('left') as HTMLElement;
    const chartEle2: HTMLElement = document.getElementById('right') as HTMLElement;
    const chart = init(chartEle);
    const chart1 = init(chartEle1);
    const chart2 = init(chartEle2);
    const option: EChartsOption = {
        dataZoom: [
            {
                id: 'dataZoomX',
                type: 'slider',
                xAxisIndex: [0],
                filterMode: 'filter'
            },
           
        ],
        title: {
            text: '攻击分类统计',
  
            left: 'center'
        },
        xAxis: {
            
            type: 'category',
            data: arr1
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data:arr2,
                type: 'bar'
            }
        ]
    };
    option && chart.setOption(option);

    // 圆饼图1
    const option1: EChartsOption = {
        title: {
            text: '难度统计',
            subtext: 'Fake Data',
            left: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            left: 'left'
        },
        series: [
            {
                name: 'Access From',
                type: 'pie',
                radius: '50%',
                data: levelarr,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            },
        ]
    };
    option1 && chart1.setOption(option1);

    // 圆饼图1
    const option2: EChartsOption = {
        title: {
            text: '文件分类统计',
            subtext: 'Fake Data',
            left: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            left: 'left'
        },
        series: [
            {
                name: 'Access From',
                type: 'pie',
                radius: '50%',
                data: categoryarr,
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            },
        ]
    };
    option2 && chart2.setOption(option2);
})
</script>


  
<style lang='less' scoped>
#app {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
}

#chart {
    width: 600px;
    height: 300px;
    margin: 0 auto;
}


.user-content {
    width: 100%;
    min-height: 500px;
    margin-top: 20px;
    background-color: white;
    padding-top: 20px;
    padding-left: 20px;
    box-sizing: border-box;

    .item {
        margin-bottom: 20px;

        .btn {
            width: 80px;
            height: 40px;
            line-height: 40px;
            border: none;
            background-color: #139DFF;
            color: aliceblue;
            margin-left: 20px;
            border-radius: 4px;
        }

        .btn1 {
            width: 80px;
            height: 40px;
            line-height: 40px;
            border: none;
            margin-left: 20px;
            border-radius: 4px;
        }

    }

    .el-input {
        width: 200px;
        height: 40px;
    }
}

.content {
    display: flex;
    justify-content: space-evenly;

    #left {
        width: 600px;
        height: 250px;

    }

    #right {
        width: 600px;
        height: 250px;

    }
}
</style>